package com.tycmc.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class RedisUtilBack {
    //Redis服务器IP
    private static final String ADDR = "192.168.30.101";

    //Redis的端口号
    private static final int PORT = 6379;

    //访问密码
    private static final String AUTH = "tykj66TYKJ";

    //可用连接实例的最大数目，默认值为8；
    //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
    private static final int MAX_ACTIVE = 1024;

    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
    private static final int MAX_IDLE = 200;

    //等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
    private static final int MAX_WAIT = 10000;

    private static final int TIMEOUT = 10000;

    //在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的；
    private static final boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    /**
     * 初始化Redis连接池
     */
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(MAX_IDLE);
            config.setMaxWaitMillis(MAX_WAIT);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Jedis实例
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                return jedisPool.getResource();
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 释放jedis资源
     * @param jedis
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedisPool.close();
        }
    }

    public static void sadd(String key, Set<String> value) {
        try {
            Jedis jedis = getJedis();
            for(String str: value){
                jedis.sadd(key, str);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static void set(String key, String value) {
        try {
            Jedis jedis = getJedis();
            jedis.set(key,value);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static void srem(String key, Set<String> value) {
        try {
            Jedis jedis = getJedis();
            Iterator<String> it = value.iterator();
            while(it.hasNext()){
                String str = it.next();
                jedis.srem(key, str);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static String get(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.get(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static Long scard(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.scard(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static Set<String> smembers(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.smembers(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static boolean sismember(String key, String value) {
        try {
            Jedis jedis = getJedis();
            return jedis.sismember(key,value);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static String srandmember(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.srandmember(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static void hmset(String key, Map<String, String> map) {
        try {
            Jedis jedis = getJedis();
            jedis.hmset(key,map);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static Long hlen(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.hlen(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static Set<String> hkeys(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.hkeys(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public static List<String> hvals(String key) {
        try {
            Jedis jedis = getJedis();
            return jedis.hvals(key);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public List<String> hmget(String key, String s1, String s2) {
        try {
            Jedis jedis = getJedis();
            return jedis.hmget(key,s1,s2);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public void hdel(String key, String s) {
        try {
            Jedis jedis = getJedis();
            jedis.hdel(key,s);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public void lpush(String key, List<String> list) {
        try {
            Jedis jedis = getJedis();
            for(String s: list){
                jedis.lpush(key,s);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public List<String> lrange(String key, Long start, Long end) {
        try {
            Jedis jedis = getJedis();
            return jedis.lrange(key, start, end);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }


    public void del(String key) {
        try {
            Jedis jedis = getJedis();
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void append(String key, String value) {
        try {
            Jedis jedis = getJedis();
            jedis.append(key,value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
